<?php
/**
 * Created by PhpStorm.
 * 196会员处理
 * User: caicm
 * Date: 2017/10/13
 * Time: 10:54
 */

namespace SDXApi\Controller;


use Think\Controller;

class SpecialMemberController extends Controller {
    protected $key = 'OjvBrEO15nNs4egBc3pLcgJ2yetskqiK';
    protected $admin_id;

    protected function _initialize(){
        header("Access-Control-Allow-Origin: *");
        header('Content-type: application/json');
        $ationarr=array('login');
        if(!in_array(ACTION_NAME, $ationarr)){
            $this->check_sign();
        }
    }

    /**
     * 196管理员登录
     * Created By caicm
     * @param name 用户名
     * @param  password 密码 MD5加密 非文明传输
     */
    public function login(){
        $user_name = I('post.user_name');
        $password = I('post.password');
        if(empty($user_name) || empty($password)){
            returnApiError('参数有误！');
        }

        $user = $this->get_amdin_user(0, $user_name);

        if(!empty($user['ec_salt'])){
            $password = md5($password . $user['ec_salt']);
        }

        if(!$user || $user['password'] != $password){
            returnApiError('账户或密码有误');
        }

        $wprow = M('warehouse_personnel')->field('position')->where(array('uid'=>$user['user_id'],'status'=>1))->find();
        if(!$wprow || $wprow['position'] != 6){
            returnApiError('该用户没有权限登录');
        }

        $data = array(
            'admin_id' => $user['user_id'],
            'admin_name' => $user['user_name'],
            'admin_sign' => $this->get_sing($user)
        );

        returnApiSuccess($data, '登录成功');
    }

    /**
     * 获取会员列表
     * Created By caicm
     * @param admin_id 管理员id
     * @param admin_sign 签名
     * @param page 分页
     */
    public function get_user_list(){

        $page = I('post.page', 1, 'int');

        $where = array(
            'u.is_891' => 2,
            'u.196_admin_id' => $this->admin_id
        );

        if($this->admin_id == 700){
            $where = array(
                'u.is_891' => 2,
                'u.196_admin_id' => array('gt', 0)
            );
        }

        $count = M('users as u')->where($where)->count();
        $data = show_page($count, $page);

        $field = 'u.user_id,u.user_name,t.sgin';
        $user_list = M('users as u')
            ->join('left join sdx_user_token as t on u.user_id=t.uid')
            ->field($field)
            ->where($where)
            ->limit(($page-1)*$data['pagesize'], $data['pagesize'])
            ->select();

        if($this->admin_id == 700){
            $admin_id = 0;
        }else{
            $admin_id = $this->admin_id;
        }

        foreach ($user_list as $k=>$v){
            $user_list[$k]['order_money'] = $this->get_today_order_money($v['user_id'], $admin_id);
            $user_list[$k]['order_num'] = $this->get_today_order_num($v['user_id'], $admin_id);
        }

        $data['user_list'] = $user_list;
        $data['order_money'] = $this->get_today_order_money(0, $admin_id);
        $data['order_num'] = $this->get_today_order_num(0, $admin_id);

        if($user_list){
            returnApiSuccess($data);
        }else{
            returnApiError('无数据');
        }

    }

    /**
     * 获取用户当天订单数量
     * Created By caicm
     * @param int $user_id
     * @param int $admin_id
     * @return int
     */
    protected function get_today_order_num($user_id = 0, $admin_id=0){
        $sta_time = gmstr2time(date('Y-m-d'));
        $where = array(
            //'wid' => 15,
            'user_id' => $user_id,
            '196_admin_id' => $admin_id,
            'pay_status' => array('in',  array(2,3)),
            'add_time' => array('gt', $sta_time),
        );
        if($user_id == 0) unset($where['user_id']);
        if($admin_id == 0){
            $where['196_admin_id'] = array('gt', 0);
        }

        $num = M('order_info')->where($where)->count();

        return $num ? $num : 0;
    }

    /**
     * 获取用户当天订单金额
     * Created By caicm
     * @param int $user_id
     * @param int $admin_id
     * @return int|string
     */
    protected function get_today_order_money($user_id = 0, $admin_id=0){
        $sta_time = gmstr2time(date('Y-m-d'));
        $where = array(
            //'wid' => 15,
            'user_id' => $user_id,
            '196_admin_id' => $admin_id,
            'pay_status' => array('in',  array(2,3)),
            'add_time' => array('gt', $sta_time),
        );
        if($user_id == 0) unset($where['user_id']);
        if($admin_id == 0){
            $where['196_admin_id'] = array('gt', 0);
        }

        $money = M('order_info')->where($where)->sum('goods_amount-discount-bonus+shipping_fee-refund_amount');

        return $money ? number_format($money, 2) : 0;
    }

    /**
     * 验证签名
     * Created By caicm
     */
    protected function check_sign(){
        $this->admin_id = I('post.admin_id');
        $admin_sign = I('post.admin_sign');

        if(empty($this->admin_id)){
            returnApiError('admin_id is null');
        }
        if(empty($admin_sign)){
            returnApiError('admin_sign is null');
        }

        $user = $this->get_amdin_user($this->admin_id);
        if($this->get_sing($user) != $admin_sign){
            returnApiError('签名有误！');
        }
    }

    /**
     * 获取管理员信息
     * Created By caicm
     * @param int $user_id
     * @param string $user_name
     * @return mixed
     */
    protected function get_amdin_user($user_id=0, $user_name=''){
        $where = array(
            'user_name' => $user_name,
            'user_id' => $user_id
        );
        if($user_id == 0) unset($where['user_id']);
        if($user_name == '') unset($where['user_name']);

        return M('admin_user')->field('user_id,user_name,ec_salt,password')->where($where)->find();
    }

    /**
     * 获取签名
     * Created By caicm
     * @param $user
     * @return string
     */
    protected function get_sing($user){
        return md5($this->key . md5($user['user_id'] . $user['user_name'] . $user['ec_salt'] . $user['password']));
    }

}